﻿using Common.Communication;
using Common.Tool.DataTools;
using DeviceBase;
using DeviceBase.Enums;
using DeviceBase.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace WanYongBiao.Core.Rigol
{
    public class DM3058ENiVisa:NiVisaBase , IWanYongBiao
    {
        public string Name { get; set; }
        public DM3058ENiVisa(string name, string sn, int timeout = 1500) : base(sn, timeout)
        {
            Name = name;
        }

        float RMin = 0;
        float RMax = 10000000;

        float VolMin = 0;
        float VolMax = 1000f;
        float CurMin = 0;
        float CurMax = 10f;
        string SetDcVol => ":FUNCtion:VOLTage:DC\r\n"; 
        string SetDcVolOk => "DCV";
        string MeasureDcVol => ":MEASure:VOLTage:DC?\r\n";
        string SetDcCur => ":FUNCtion:CURRent:DC\r\n";
        string SetDcCurOk => "DCI";
        string MeasureDcCur => ":MEASure:CURRent:DC?\r\n";
        string Set4wr => ":FUNCtion:FRESistance\r\n";
        string Set4wrOk => "4WR";
        string Measure4wr => ":MEASure:FRESistance?\r\n";
        string Set2wr => ":FUNCtion:RESistance\r\n";
        string Set2wrOk => "2WR";
        string Measure2wr => ":MEASure:RESistance?\r\n";

        string SetCont => ":FUNCtion:CONTinuity\r\n";
        string SetContOk => "CONT";
        string MeasureCont => ":MEASure:CONTinuity?\r\n";
        string GetFunc => ":FUNCtion?\r\n";

        public Result<double> GetVoltage(WanYongBiaoEnum.VoltageMode mode = WanYongBiaoEnum.VoltageMode.DC)
        {
            Result<double> result = new Result<double>();
            if (mode == WanYongBiaoEnum.VoltageMode.DC)
            {
                AppendInfo(Name, $"打开直流电压测量");
                var order = SetDcVol;
                var re = Write(order);
                if (!re.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开直流电压测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                order = GetFunc;
                //发送指令并返回
                var re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !re2.Value.Contains(SetDcVolOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开直流电压测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电压");
                order = MeasureDcVol;
                double vol = 0f;
                //发送指令并返回
                re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !double.TryParse(re2.Value, out vol))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电压失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = vol;
                AppendResult(Name, DataConvert.ToUnit(vol, "F3") + "V");
            }
            if (mode == WanYongBiaoEnum.VoltageMode.AC)
            {
                result.Err = "暂不支持";
                result.IsSucceed = false;
            }
            return result.EndTime();
        }
        public Result<double> GetCurrent(WanYongBiaoEnum.CurrentMode mode=WanYongBiaoEnum.CurrentMode.DC)
        {
            Result<double> result = new Result<double>();
            if (mode == WanYongBiaoEnum.CurrentMode.DC)
            {
                AppendInfo(Name, $"打开直流电流测量");
                var order = SetDcCur;
                var re = Write(order);
                if (!re.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开直流电流测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                order = GetFunc;
                //发送指令并返回
                var re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !re2.Value.Contains(SetDcCurOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开直流电流测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电流");
                order = MeasureDcCur;
                double cur = 0f;
                //发送指令并返回
                re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !double.TryParse(re2.Value, out cur))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电流失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = cur;
                AppendResult(Name, DataConvert.ToUnit(cur, "F3") + "A");
            }
            if (mode == WanYongBiaoEnum.CurrentMode.AC)
            {
                result.Err = "暂不支持";
                result.IsSucceed = false;
            }
            return result.EndTime();
        }

        public Result<double> GetResistance(WanYongBiaoEnum.ResistanceMode mode = WanYongBiaoEnum.ResistanceMode._2WR)
        {
            Result<double> result = new Result<double>();
            if (mode == WanYongBiaoEnum.ResistanceMode._2WR)
            {
                AppendInfo(Name, $"打开二线电阻测量");
                var order = Set2wr;
                var re = Write(order);
                if (!re.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开二线电阻测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                order = GetFunc;
                //发送指令并返回
                var re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !re2.Value.Contains(Set2wrOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开二线电阻测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电阻");
                order = Measure2wr;
                double r = 0f;
                //发送指令并返回
                re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !double.TryParse(re2.Value, out r))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电阻失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = r;
                AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
            }
            if (mode == WanYongBiaoEnum.ResistanceMode._4WR)
            {
                AppendInfo(Name, $"打开四线电阻测量");
                var order = Set4wr;
                var re = Write(order);
                if (!re.IsSucceed)
                {
                    result.IsSucceed = false;
                    result.Err = "打开四线电阻测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                Thread.Sleep(100);
                order = GetFunc;
                //发送指令并返回
                var re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !re2.Value.Contains(Set4wrOk))
                {
                    result.IsSucceed = false;
                    result.Err = "打开四线电阻测量失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                AppendInfo(Name, $"读取当前电阻");
                order = Measure2wr;
                double r = 0f;
                //发送指令并返回
                re2 = WriteThenRead(order);
                if (!re2.IsSucceed || !double.TryParse(re2.Value, out r))
                {
                    result.IsSucceed = false;
                    result.Err = "读取当前电阻失败";
                    AppendResult(Name, result.Err);
                    return result.EndTime();
                }
                result.Value = r;
                AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
            }
            return result.EndTime();
        }
        public Result<double> GetContinuity()
        {
            Result<double> result = new Result<double>();
            AppendInfo(Name, $"打开通断测量");
            var order = SetCont;
            var re = Write(order);
            if (!re.IsSucceed)
            {
                result.IsSucceed = false;
                result.Err = "打开通断测量失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            Thread.Sleep(100);
            order = GetFunc;
            //发送指令并返回
            var re2 = WriteThenRead(order);
            if (!re2.IsSucceed || !re2.Value.Contains(SetContOk))
            {
                result.IsSucceed = false;
                result.Err = "打开通断测量失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            AppendInfo(Name, $"读取当前电阻");
            order = MeasureCont;
            double r = 0f;
            //发送指令并返回
            re2 = WriteThenRead(order);
            if (!re2.IsSucceed || !double.TryParse(re2.Value, out r))
            {
                result.IsSucceed = false;
                result.Err = "读取当前电阻失败";
                AppendResult(Name, result.Err);
                return result.EndTime();
            }
            result.Value = r;
            AppendResult(Name, DataConvert.ToUnit(r, "F3") + "Ω");
            return result.EndTime();
        }

    }
}
